package com.yanfan.product.controller;


import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.*;
import com.yanfan.energy.service.BatchSegmentationService;
import com.yanfan.energy.service.ExcelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 生产记录表(BatchSegmentation)表控制层
 *
 * @author makejava
 * @since 2023-01-10 09:14:25
 */
@RestController
@RequestMapping("/production/productionRecords")
public class BatchSegmentationController {
    /**
     * 服务对象
     */
    @Resource
    private BatchSegmentationService batchSegmentationService;

    @Autowired
    private ExcelService excelService;

    /**
     * 分页查询
     *
     * @param batchSegmentation 筛选条件
     * @return 查询结果
     */
    @PostMapping("/queryByPage")
    public AjaxResult queryByPage(@RequestBody BatchSegmentationRequest batchSegmentation) {

        PageInfo<BatchSegmentation> pageInfo = new PageInfo<>(batchSegmentationService.queryByPage(batchSegmentation));
        return AjaxResult.success(pageInfo);
    }

    /**
     * 生产记录页面List接口
     *
     * @param batchSegmentation 筛选条件
     * @return 查询结果
     */
    @PostMapping("/queryList")
    public AjaxResult queryList(@RequestBody BatchSegmentationRequest batchSegmentation) {
        PageHelper.startPage(batchSegmentation.getCurrPage(), batchSegmentation.getPageSize());
        PageInfo<ProductionRecordResponse> pageInfo = new PageInfo<>(batchSegmentationService.queryList(batchSegmentation));
        return AjaxResult.success(pageInfo);
    }


    /**
     * 下载
     *
     * @return 查询结果
     */
    @PostMapping("/queryListReport")
    public void queryListReport(@RequestParam(required = false) String batchNumber, @RequestParam(required = false) String segmentNumber, @RequestParam(required = false) String craftName, @RequestParam(required = false) Long factoryId, @RequestParam(required = false) Long workshopId, @RequestParam(required = false) String date, HttpServletResponse response) throws IOException, ParseException {
        BatchSegmentationRequest batchSegmentationRequest = new BatchSegmentationRequest();
        batchSegmentationRequest.setBatchNumber(batchNumber);
        batchSegmentationRequest.setSegmentNumber(segmentNumber);
        batchSegmentationRequest.setCraftName(craftName);
        batchSegmentationRequest.setFactoryId(factoryId);
        batchSegmentationRequest.setWorkshopId(workshopId);
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            batchSegmentationRequest.setFinishDate(sdf.parse(date));
        }
        List<ProductionRecordResponse> list = this.batchSegmentationService.queryList(batchSegmentationRequest);
        List<BatchSegmentationHead> exportData = batchSegmentationService.getExportData(list);
        excelService.downloadList(BatchSegmentationHead.class, exportData, response);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryById/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.batchSegmentationService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param batchSegmentation 实体
     * @return 新增结果
     */
    @PostMapping("/addRecords")
    public AjaxResult add(@RequestBody BatchSegmentation batchSegmentation) throws Exception {
        return AjaxResult.success(this.batchSegmentationService.insert(batchSegmentation));
    }

    /**
     * 编辑数据
     *
     * @param batchSegmentation 实体
     * @return 编辑结果
     */
    @PostMapping("/editRecords")
    public AjaxResult edit(@RequestBody BatchSegmentation batchSegmentation) throws Exception {
        return AjaxResult.success(this.batchSegmentationService.update(batchSegmentation));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/deleteById/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.batchSegmentationService.deleteById(id));
    }

    /**
     * 生产管理生产记录的生产统计图表
     *
     * @param request
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author caijinkang
     * @date 2023/4/11 15:56
     */
    @PostMapping("/productionRecordsChart")
    public AjaxResult productionRecordsChart(@RequestBody ProductionRecordsChartRequest request) {
        return AjaxResult.success(batchSegmentationService.productionRecordsChart(request));
    }

    @PostMapping("/stateList")
    public AjaxResult stateList() {
        return AjaxResult.success(batchSegmentationService.stateList());
    }


}

